<?
class usersAjaxLog extends ActionHandler
{
    function log_list()
    {
        global $user, $dbLink, $lang, $module;
        if (!$user->isEnabledTo("view_log", $module->id)) die();
        $data = array();

        $table = "admin_logs AS ul LEFT JOIN users AS u ON ul.userID = u.userID ";

        $condition = " WHERE 1 ";

        if(isset($_REQUEST["module"]) && $_REQUEST["module"] !="")
            $condition .= " AND module = '".mysql_escape_string($_REQUEST["module"])."' ";

        if(isset($_REQUEST["user"]) && $_REQUEST["user"] !="")
            $condition .= " AND ul.userID = '".mysql_escape_string($_REQUEST["user"])."' ";

        if(isset($_REQUEST["datef"]) && $_REQUEST["datef"] !="")
            $condition .= " AND  DATE(ul.date_created) >= '".mysql_escape_string($_REQUEST["datef"])."' ";

        if(isset($_REQUEST["datet"]) && $_REQUEST["datet"] !="")
            $condition .= " AND  DATE(ul.date_created) <= '".mysql_escape_string($_REQUEST["datet"])."' ";


        $sql = "SELECT count(*) AS cant FROM ".$table.$condition. " ORDER BY ul.logID DESC ";
        list($numRows) = mysql_fetch_array(mysql_query($sql, $dbLink));
        $totalRows = $numRows;

        if ($numRows > 0) {

            $sql = "SELECT ul.*, IF(u.username IS null, '< no user >', u.username) as username  FROM ".$table.$condition. " ORDER BY ul.logID DESC  ";
            $sql .= " LIMIT ". (int) $_POST['start'] .", ". ((int)$_POST['limit']<$module->numMaxListedRows&&(int)$_POST['limit']!=0? (int)$_POST['limit'] : $module->numMaxListedRows);

            $res = mysql_query($sql, $dbLink);
        }

        // showing results
        $data = array();
        if ($numRows > 0) {
            while($row = mysql_fetch_assoc($res))
            {
                switch($row['status'])
                {
                    case ADMIN_LOG_STATUS_OK: $row['status'] = $lang->translate('ok'); break;
                    case ADMIN_LOG_STATUS_ERROR: $row['status'] = $lang->translate('error'); break;
                    case ADMIN_LOG_STATUS_DONE: $row['status'] = $lang->translate('done'); break;
                    case ADMIN_LOG_STATUS_CANCELED: $row['status'] = $lang->translate('started'); break;
                    case ADMIN_LOG_STATUS_STARTED: $row['status'] = $lang->translate('pending'); break;
                    case ADMIN_LOG_STATUS_PENDING: $row['status'] = $lang->translate('canceled'); break;
                    case ADMIN_LOG_STATUS_FINISHED: $row['status'] = $lang->translate('finished'); break;
                    default: $row['status'] = '';
                }
                $data[] = $row;
            }
        }

        echo json_encode(array("data"=> $data, "total"=>$totalRows));
    }
}

?>
